<template>
  <div class="box">
    <div class="login">
      <div id="loginvue">
        <img src="../../assets/作业资料/pic/logo/orange@3x.png" alt="" />
        <p>优 质 甄 选 · 高 枕 无 忧</p>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="username" class="text">
            <el-input
              v-model="ruleForm.username"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password" class="text">
            <el-input
              v-model="ruleForm.password"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="submitForm('ruleForm')"
              type="danger"
              round
              class="button"
              >立即创建</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  username: "",
  components: {},
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名·", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$api
            .postLogin(this.ruleForm)
            .then((res) => {
              localStorage.setItem("token", res.data.list.token);
              console.log(res);
              this.$message({
                message: "登陆成功",
                type: "success",
                duration: "500",
              });
              this.$router.push("/index");
            })
            .catch((err) => console.log(err));
        } else {
          this.$message({
            message: "登录失败,请重新登录",
            duration: "500",
            type: "error",
          });
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.box {
  width: 100%;
  height: 100%;
  background-color: #ff694d;
}
.login {
  width: 700px;
  height: 550px;
  position: relative;
  left: 40%;
  top: 15%;
}
#loginvue {
  width: 100%;
  height: 100%;
  background: url("../../assets/作业资料/pic/bg/login@3x.png");
  background-size: 700px 550px;
  img {
    width: 350px;
    height: 100px;
    position: relative;
    left: 320px;
    top: 100px;
  }
  .text {
    width: 350px;
    position: relative;
    left: 320px;
    top: 200px;
  }
  p {
    position: relative;
    left: 420px;
    top: 150px;
    color: grey;
  }
  .button {
    width: 350px;
    position: relative;
    left: 320px;
    top: 200px;
  }
}
</style>
